package pubM

import (
	"fmt"
	"db2s/arg"
	"db2s/outPut"
	"db2s/ref"
	"os"
	"reflect"
	"time"
)

const (
	BreakPointFileName       = "breakPointFinish.point"
	ParameterRecord          = "parameterRecord"
	MonitorBarFinishFileName = "ttyBarFinish"
)

// dirInit  目录初始化
func dirInit(path string) (err error) {
	if _, err = os.Stat(path); err == nil {
		return
	}
	if !os.IsNotExist(err) {
		return
	}
	if err = os.MkdirAll(path, os.ModePerm); err != nil {
		err = ref.ErrAddPrintf("dirInit", err)
		return
	}
	return
}

//func newXlsObject(baseDir string, config GlobalParametersConfig) {
//	xlsFileName := fmt.Sprintf("%v/%v", baseDir, config.result.XlsV.XlsFileName)
//	if !strings.Contains(xlsFileName, "/") {
//		xlsFileName = fmt.Sprintf("./%v", xlsFileName)
//	}
//	xlsFile = outPut.NewFileOperations(outPut.Xls{
//		FileName:  fmt.Sprintf("./%v.xlsx", xlsFileName),
//		SheetName: []string{"terminal", "result", "miss"},
//		Mode:      config.rules.CheckMode,
//		Enroll:    []any{outPut.WorkSheetTerminal{}, outPut.WorkSheetResult{}, outPut.WorkSheetMiss{}},
//		Safety:    config.result.XlsV.XlsSafetySwitch,
//	})
//}
//func initTextData(baseDir string, config GlobalParametersConfig) (err error) {
//	textFile = outPut.NewFileOperations(outPut.Tex{
//		SheetName: []string{"terminal", "result", "miss"},
//		Mode:      config.rules.CheckMode,
//		Enroll:    []any{outPut.WorkSheetTerminal{Persistence: baseDir}, outPut.WorkSheetResult{Persistence: baseDir}, outPut.WorkSheetMiss{Persistence: baseDir}},
//		Safety:    false,
//	})
//	if err = textFile.Head(); err != nil {
//		return
//	}
//	return
//}
//func initXlsData(baseDir string, config GlobalParametersConfig) (err error) {
//	newXlsObject(baseDir, config)
//	if err = xlsFile.Create(); err != nil {
//		return
//	}
//	if err = xlsFile.Head(); err != nil {
//		return
//	}
//	return
//}

func syncBreakPointF(config arg.RulesS) bool {
	switch config.Sync.Breakpoint {
	case false:
		return true
	case true:
		return false
	}
	return false
}
func breakPointFuncMap() map[string]reflect.Value {
	return map[string]reflect.Value{"sync": reflect.ValueOf(syncBreakPointF)}
}
func getBreakPointStatus(config arg.RulesS) bool {
	if funcName, ok := breakPointFuncMap()[config.CheckMode]; ok {
		args := []reflect.Value{
			reflect.ValueOf(config),
		}
		l := funcName.Call(args)
		return l[0].Interface().(bool)
	}
	return false
}
func initBreakPoint(path string, m arg.ConfigParameter) (breakPoint BreakActionEr, err error) {
	breakPoint = NewBreakObject(BreakPoint{
		FileName: fmt.Sprintf("%v/%v", path, BreakPointFileName),
		Truncate: getBreakPointStatus(m.SecondaryL.RulesV),
	})
	if err = breakPoint.Read(); err != nil {
		err = ref.ErrAddPrintf("initBreakPoint", err)
		return
	}
	return
}
func initParameterPoint(path string) (ParameterPoint *os.File, err error) {
	if ParameterPoint, err = os.OpenFile(fmt.Sprintf("%v/%v", path, ParameterRecord), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0664); err != nil {
		err = ref.ErrAddPrintf("initParameterPoint", err)
		return
	}
	return
}
func initTtyResPoint(path string) (newTextTPod outPut.FileOperations, err error) {
	newTextTPod = outPut.NewFileOperations(outPut.Tex{FileName: fmt.Sprintf("%v/%v.data", path, MonitorBarFinishFileName)})
	if err = newTextTPod.Init(); err != nil {
		err = ref.ErrAddPrintf("initTtyResPoint", err)
		return
	}
	return
}
func initResDataPoint(path string) (newTextTPod outPut.FileOperations, err error) {
	return
}
func getResultDataPath(baseDir string, m arg.ConfigParameter) (pathGarth map[string]string, err error) {
	//初始化输出目录result
	var name = "eoms1"
	var timeStr = time.Now().Format("2006-01-02T15-04-05")
	baseDir = "result"
	pathGarth = make(map[string]string)
	for _, v := range []string{"breakPoint", "parameter", "kill", "ttyRes", "ResData"} {
		var newBaseDir string
		switch v {
		case "breakPoint", "parameter", "kill":
			newBaseDir = fmt.Sprintf("%v/%v/%v/%v", baseDir, name, v, m.SecondaryL.RulesV.CheckMode)
		default:
			newBaseDir = fmt.Sprintf("%v/%v/%v/%v/%v", baseDir, name, v, m.SecondaryL.RulesV.CheckMode, timeStr)
		}
		if err = dirInit(newBaseDir); err != nil {
			err = ref.ErrAddPrintf("getResultDataPath", err)
			return
		}
		pathGarth[v] = newBaseDir
	}
	return
}

type RunDataPoint struct {
	XlsFile        outPut.FileOperations
	TtyFile        outPut.FileOperations
	NewCell        outPut.StreamRes
	BreakPoint     BreakActionEr
	ParameterPoint *os.File
	Socket         string
}

func InitResultData(baseDir string, m arg.ConfigParameter) (res RunDataPoint, err error) {
	var pathGarth map[string]string
	if pathGarth, err = getResultDataPath(baseDir, m); err != nil {
		err = ref.ErrAddPrintf("InitResultData", err)
		return
	}
	for k, v := range pathGarth {
		switch k {
		case "breakPoint":
			if res.BreakPoint, err = initBreakPoint(v, m); err != nil {
				err = ref.ErrAddPrintf("InitResultData", err)
				return
			}
		case "parameter":
			if res.ParameterPoint, err = initParameterPoint(v); err != nil {
				err = ref.ErrAddPrintf("InitResultData", err)
				return
			}
		case "kill":

		case "ttyRes":
			if res.TtyFile, err = initTtyResPoint(v); err != nil {
				err = ref.ErrAddPrintf("InitResultData", err)
				return
			}
		case "ResData":
			if _, err = initResDataPoint(v); err != nil {
				err = ref.ErrAddPrintf("InitResultData", err)
				return
			}
		}

	}
	//if initXlsData(baseDir, config) != nil {
	//	return
	//}
	//if initTextData(baseDir, config) != nil {
	//	return
	//}
	return
}
